package main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;

public class MainPanel extends JPanel {
	private static final int GRID_SIZE = 20;
	private int[] SIZE = new int[] { 30, 30 };
	private Field field;

	public MainPanel() {
		field = new Field(SIZE[0], SIZE[1]);
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				field.keyboard(e);
			}
		});
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		int[][] grid = field.step();
		for (int row = 0; row < grid.length; row++) {
			for (int each = 0; each < grid[row].length; each++) {
				g.setColor(new Color(0, 0, 0, 100));
				g.drawRect(each * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE);
				switch (grid[row][each]) {
					case 0:
						g.setColor(new Color(50, 210, 230));
						break;
					case 2:
						g.setColor(new Color(183, 57, 224));
						break;
					case 1:
						g.setColor(new Color(70, 20, 45));
						break;
				}
				g.fillRect(each * GRID_SIZE + 1, row * GRID_SIZE + 1, GRID_SIZE, GRID_SIZE);
			}
		}
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(SIZE[0] * GRID_SIZE + 1,
				SIZE[1] * GRID_SIZE + 1);
	}

	public int getScore() {
		return field.getScore();
	}
}